package net.messagenet.mazor.web.mngbeans;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.messagenet.mazor.DO.DpaDO;
import net.messagenet.mazor.DO.DpaProductDO;
import net.messagenet.mazor.ejb.facade.MZRFacadeLocal;
import net.messagenet.mazor.exception.MZRApplicativeException;
import net.messagenet.mazor.exception.MZRInternalServerException;

import org.ajax4jsf.model.KeepAlive;
import org.apache.log4j.Logger;


@KeepAlive
public class OpenReportsManagedBean implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5388230760485952510L;
	private String selectedDpaIdFormTable;
	private int selectedProdFormTable;
	private List<DpaProductDO> productList;

	//	private UserCacheManager cacheManager;
	private MZRFacadeLocal facade=null;
	

	private Logger logger = Logger.getLogger(this.getClass());

	private HashMap<Integer, DpaProductDO> prodMap;
	
	private Map<Integer,DpaDO> dpaMap;

	private List<DpaDO> dpaList;
	

	public String getSelectedDpaIdFormTable() {
		return selectedDpaIdFormTable;
	}

	public void setDpaMap(Map<Integer, DpaDO> dpaMap) {
		this.dpaMap = dpaMap;
	}

	public void setSelectedDpaIdFormTable(String id) {
		this.selectedDpaIdFormTable = id;
	}

	

	public List<DpaDO> getDpaList() {
		return dpaList;
	}

	public void setDpaList(List<DpaDO> dpaList) {
		this.dpaList = dpaList;
	}



	
	@PostConstruct
	public void init() throws MZRInternalServerException, MZRApplicativeException {
		
		InitialContext ic;
			
		try {
				ic = new InitialContext();
				facade = (MZRFacadeLocal) ic.lookup("MZRFacade/local");
			} catch (NamingException e) {
				logger.error(e.getMessage(),e);
			}
			
			
			
			productList= new LinkedList<DpaProductDO>();
			dpaMap=facade.getAllNotClosedDPAs();
			dpaList= new LinkedList<DpaDO>();
			Iterator<Entry<Integer, DpaDO>> it = dpaMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<Integer, DpaDO> entry = (Map.Entry<Integer, DpaDO>) it
						.next();
				dpaList.add(entry.getValue());
			}
	}

    /**
     * 
     * This method is called when the user chose a DPA and it fills the products table.
     * 
     */
	public void selectDpaFromTable()
	{
		DpaDO dpaSelected=dpaMap.get(Integer.parseInt(selectedDpaIdFormTable));
		productList=dpaSelected.getDpaProducts();
		//making a map of the products
		prodMap = new HashMap<Integer,DpaProductDO>();
		for (DpaProductDO p : productList) {
			prodMap.put(p.getProduct().getId(), p);
		}
	}
	
	public List<DpaProductDO> getProductList() {
		return productList;
	}


	public void setProductList(List<DpaProductDO> productList) {
		this.productList = productList;
	}


	public Map<Integer, DpaDO> getDpaMap() {
		return dpaMap;
	}


	public void setDpaList(Map<Integer, DpaDO> dpaMap) {
		this.dpaMap = dpaMap;
	}

	public int getSelectedProdFormTable() {
		return selectedProdFormTable;
	}

	public void setSelectedProdFormTable(int selectedProdFormTable) {
		this.selectedProdFormTable = selectedProdFormTable;
	}
}

